<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
$MAIN->users->checkLogin(true);

if(isset($_REQUEST['status'])){
	if($MAIN->registrations->toggleRegistrartionStatus($_REQUEST['reg'],$_REQUEST['status'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=registrations&view=all';}).delay(1000);</script>
	<?php
	};
	exit();
}

if(isset($_REQUEST['del'])){
	if($MAIN->registrations->delRegistration($_REQUEST['del'])){
	?>
	<script type="text/javascript">sendInfo(null,'Dina ändringar har sparats!');(function(){document.location.href='index.php?cat=registrations&view=all';}).delay(1000);</script>
	<?php	
	};	
	exit();
}
?>
<div class="activ">
<?php
switch(@$_REQUEST['view']){
	default:
	case 'all':
	$ord="ASC";
	$orderBy="reg_date";
	if(isset($_REQUEST['ordby'])){$orderBy=$_REQUEST['ordby'];}
	$nextOrd="DESC";
	if(isset($_REQUEST['ord'])){$ord=$_REQUEST['ord'];$nextOrd =($_REQUEST['ord']=="ASC")?"DESC":"ASC";}
	$registrations = $MAIN->registrations->getRegistrations($orderBy,$ord);
?>
	<h1>Alla registrerade föranmälningar</h1>
	<table cellpadding="0" cellspacing="0" class="list">
	<tr>
		<th><a href="?cat=registrations&amp;view=all&amp;ordby=reg_date&amp;ord=<?php echo($nextOrd);?>">Datum</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=registrations&amp;view=all&amp;ordby=circle_name&amp;ord=<?php echo($nextOrd);?>">Studiecirkel</a></th>
		<th style="width:10px;"></th>
		<th><a href="?cat=registrations&amp;view=all&amp;ordby=leader_name&amp;ord=<?php echo($nextOrd);?>">Ledare</a></th>
		<th style="width:10px;"></th>		
		<th><a href="?cat=registrations&amp;view=all&amp;ordby=status&amp;ord=<?php echo($nextOrd);?>">Status</a></th>
		<th style="width:10px;"></th>		
		<th>Administrera</th>
	</tr>
	<?php
	foreach($registrations as $reg){
	?>
	<tr class="entry">		
		<td><?php echo($reg['reg_date']);?></td>
		<td></td>
		<td><?php echo($reg['type']);?></td>
		<td></td>
		<td><?php echo($reg['leader_name']);?></td>
		<td></td>
		<td>
			<?php
			if($reg['status']>1 && $MAIN->currentUser['grouplevel']>=70){
			?>
			<input style="color:#fff;background-color:<?php echo($MAIN->registrations->statusType[$reg['status']][1]);?>;" type="button" name="btn_status" value="<?php echo($MAIN->registrations->statusType[$reg['status']][0]);?>"/>
			<?php	
			}elseif($reg['status']==4){
			?>
			<input onclick="toggleStatus(<?php echo($reg['id']);?>,1);" style="color:#fff;background-color:<?php echo($MAIN->registrations->statusType[$reg['status']][1]);?>;" type="button" name="btn_status" value="<?php echo($MAIN->registrations->statusType[$reg['status']][0]);?>"/>
			<?php
			}else{
			?>
			<input onclick="toggleStatus(<?php echo($reg['id']);?>,<?php if($reg['status']+1<=3){echo($reg['status']+1);}else{echo($reg['status']);};?>);" style="color:#fff;background-color:<?php echo($MAIN->registrations->statusType[$reg['status']][1]);?>;" type="button" name="btn_status" value="<?php echo($MAIN->registrations->statusType[$reg['status']][0]);?>"/>
			<?php	
			}
			?>
			
		</td>
		<td></td>
		<td class="tools">
			<?php
			if($reg['status']>1 && $MAIN->currentUser['grouplevel']>=70){}else{
			?>
			<a href="javascript:void(0);" onclick="delRegistration(<?php echo($reg['id']);?>)"><img src="images/btn_admin_del.png" alt="Ta Bort" title="Ta Bort"/></a>			
			<a href="?cat=registrations&amp;view=set&amp;id=<?php echo($reg['id']);?>"><img src="images/btn_admin_edit.png" alt="Redigera" title="Redigera"/></a>
			<a href="pdf.php?id=<?php echo($reg['id']);?>" target="_blank" alt="Ladda när Föranmälning" title="Ladda när Föranmälning"><img src="images/btn_file_download.png" alt="Ladda när Föranmälning" title="Ladda när Föranmälning"/></a>
			<?php
			if($MAIN->registrations->isStudyPlan($reg['id'])){
				?>
			<a href="study_pdf.php?id=<?php echo($reg['id']);?>" target="_blank" alt="Ladda när Studiecirkelplan" title="Ladda när Studiecirkelplan"><img src="images/btn_file_download.png" alt="Ladda när Studiecirkelplan" title="Ladda när Studiecirkelplan"/></a>	
				<?php
			}
			if($MAIN->currentUser['grouplevel']<=50 && ($reg['status']==3 || $reg['status']==2)){
			?>
			<a href="javascript:toggleStatus(<?php echo($reg['id']);?>,4);"><img src="images/btn_send2.jpg" alt="Skicka dokument tillbaka" title="Skicka dokument tillbaka"/></a>
			<?php
			}
			}
			?>
		</td>
	</tr>
	<?php
	}
	?>
	</table>
	<script type="text/javascript">
		var delRegistration = function(id){
			var msg='Bekräfta borttagning';
			if(confirm(msg)){
				ajaxGet('registrations.php','del='+id);
			}
		}
		var toggleStatus = function(regId,status){
			var msg='Bekräfta ändring';
			if(confirm(msg)){
				ajaxGet('registrations.php','reg='+regId+'&status='+status);
			}
		}
	</script>
<?php
	break;
	case 'set':
	case 'add':
		$registration=$MAIN->registrations->getRegistration($_REQUEST['id']);
?>
<label for="ddl_circle_type">Typ : </label>
<select <?php if($registration['id']!=0){?>disabled="disabled" <?php }?> name="ddl_circle_type" id="ddl_circle_type" onchange="if(this.value!='null'){ajaxGet('forms.php','type='+this.value+'&v=set&id=0',$('registration'));}">
	<option value="null">-- ange --</option>
	<?php
	foreach($MAIN->circles->circlesType as $type){
	?>
	<option value="<?php echo($type);?>" <?php if($registration['type']==$type){?>selected="selected"<?php }?>><?php echo($type);?></option>
	<?php
	}
	?>
</select>
<div id="registration">
	<?php
	if($registration['id']!=0){
		$_REQUEST['v']=$_REQUEST['view'];
		$_REQUEST['type']=$registration['type'];
		include_once("forms.php");
	}
	?>
</div>
<?php
	break;
}
?>
</div>